#!/bin/bash

DIR_NAME="install_skyeye"
mkdir $DIR_NAME
cd $DIR_NAME
ROOT_DIR=`pwd`

#axel -n 8 "http://sourceforge.jp/frs/g_redir.php?m=jaist&f=%2Fskyeye%2Fubuntu_testcase%2Fubuntu_skyeye.tar.bz2" -o ubuntu_skyeye.tar.bz2 &
echo 下载skyeye
axel -n 8 "http://sourceforge.jp/frs/g_redir.php?m=jaist&f=%2Fskyeye%2Fskyeye%2Fskyeye-1.3.5%2Fskyeye-1.3.5_rc1.tar.bz2" -o skyeye-1.3.5_rc1.tar.bz2 > /dev/null 2>&1 &

echo 下载Python-2.7.6.tar.xz
wget https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz  > /dev/null 2>&1 &

echo 下载libiconv
axel -n 8 http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz  > /dev/null 2>&1 &

install_apt ()
{
	apt-fast install $@ -y --force-yes
	if [ $? != 0 ]; then
		echo -e "\e[5m\e[31mERROR: can't install $@\e[0m"
		exit 1
	fi
}

install_apt python-dev
install_apt libltdl-dev
install_apt libx11-dev
install_apt libxext-dev
install_apt x11proto-xext-dev
install_apt libgtk2.0-dev



#解决头文件错误
ls /usr/include/cairo/ | awk '{print "sudo ln -s /usr/include/cairo/"$1" /usr/include/"$1 }'| bash
sudo ln -s /usr/include/pango-1.0/pango /usr/include/pango
sudo ln -s /usr/lib/i386-linux-gnu/gtk-2.0/include/gdkconfig.h /usr/include/gdkconfig.h
sudo ln -s /usr/include/gdk-pixbuf-2.0/gdk-pixbuf /usr/include/gdk-pixbuf
sudo ln -s /usr/include/atk-1.0/atk /usr/include/atk

#等待下载完毕
echo -e "\e[32m\t Waiting for skyeye download over...\e[0m"
downloading=true
while downloading
do
	PROSS=`ps -el| grep axel | awk '{print $14}'`
	if [[ $PROSS = "axel" ]]; then
		downloading=true
		sleep 1
	else
		ddownloading=false
	fi
done
echo -e "\e[32m\t skyeye download OK!\e[0m"

#安装libiconv-1.14.tar.gz 		//need changed
cd $ROOT_DIR
tar -xvf libiconv-1.14.tar.gz
cd libiconv-1.14/
./configure --prefix=/usr/local
make				#有错误，见下面解决方法
if [ $? != 0 ] ; then
	sed -i 's/^_GL_WARN_ON_USE (gets,.*$/#if defined(__GLIBC__) \&\& !defined(__UCLIBC__) \&\& !__GLIBC_PREREQ(2, 16)\n_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");\n#endif/' ./srclib/stdio.in.h
	make
fi
if [ $? != 0 ]; then
	echo -e "\e[5m\e[31m:ERROR:need check!!!!\e[0m"
	exit 1
fi
sudo make install



#安装Python-2.7.6.tar.xz
cd $ROOT_DIR
echo -e "\e[32m\t Waiting for Python-2.7.6.tar.xz download over...\e[0m"
downloading=true
while downloading
do
	PROSS=`ps -el| grep wget | awk '{print $14}'`
	if [[ $PROSS = "wget" ]]; then
		downloading=true
		sleep 1
	else
		ddownloading=false
	fi
done
echo -e "\e[32m\t Python-2.7.6.tar.xz download OK!\e[0m"
tar -xvf Python-2.7.6.tar.xz
cd Python-2.7.6/
./configure --enable-shared --enable-unicode=ucs4
if [ $? != 0 ]; then
	echo -e "\e[5m\e[31m:ERROR:need check!!!!\e[0m"
	exit 1
fi
make
if [ $? != 0 ]; then
	echo -e "\e[5m\e[31m:ERROR:need check!!!!\e[0m"
	exit 1
fi
sudo make install
if [ $? != 0 ]; then
	echo -e "\e[5m\e[31m:ERROR:need check!!!!\e[0m"
	exit 1
fi


#安装skyeye
cd $ROOT_DIR
tar -xvf skyeye-1.3.5_rc1.tar.bz2
cd skyeye-1.3.5_rc1/
./configure
make lib
make
sudo make install
sudo make install_lib
echo "export PATH=\$PATH:/opt/skyeye/bin" >> ~/.bashrc

